import React from 'react'
import _ from 'underscore'
import $ from 'jquery'
import dispatcher from 'lib/dispatcher'
import {Scope} from 'lib/scopeFactory'
import basicInfo from '../apis/basicInfo'
import combOptionApis from '../apis/combOptions'
import format from 'lib/util/format'
import TimeStore from 'lib/stores/time-store'
import {combAnalysisApi,combAnalysisDetailApi,sortMap,parseToChart,propConfig} from '../apis/core-util'
import dimOptionMap from './dimOptionMap.es6'
import filterConditionMap from './filterConditionMap.es6'
import viewGridCols from './gridCols.es6'
import indicatorOptions from './indicatorOptions.es6'
import crashNameFormat from '../apis/crashNameFormat'
export default class MainScope extends Scope {
  constructor(){
    super();
    var category = indicatorOptions[0].value.category
    this.$state = {
      params:{
        urlName:indicatorOptions[0].value.urlName,
        viewKey:indicatorOptions[0].value.viewKey,
        classify:dimOptionMap[category][0].value,
        limit:5
      },
      category:category,
      indicatorOptions:indicatorOptions,
      dimOptions:dimOptionMap[category],
      filterConditions:filterConditionMap[category],
      limitOption:_.chain(0).range(10).map(function(v){return {label:(v+1)+'',value:(v+1)} }).value(),
      pie:{
        loading:true
      },
      chart:{
        loading:true
      },
      grid:{
        loading:true
      },
      gridCols:[],
      detailGrid:{
        loading:true
      },
      detailGridCols:[]
    }
  }
  fetch(){
    this.combAnalysisApi = combAnalysisApi(() => {
      return this.$state.params
    }
    , TimeStore.getValue
    , function (){
        return {
          order:sortMap[this.viewKey]
        }
      }
    )
    this.combAnalysisApi.fetch().then((data,params) => {
      this.$state.pie = {
        data:parseToChart(_.extend({},data,{
          result:_(data.result).chain().filter((r) => {
            return r.combData.val !== 0
          }).map(function(r){
            return _.extend({},r,{
              data:[r.combData]
            })
          }).value()
        }),params)
      }
      this.$state.chart = {
        data:parseToChart(data,params)
      }
      this.emit()
    },(err) => {
      this.$state.pie = {
        error:err
      }
      this.$state.chart = {
        error:err
      }
      this.emit()
    },(params) => {
      this.$state.pie = {
        loading:true
      }
      this.$state.chart = {
        loading:true
      }
      this.emit()
    });

    this.combAnalysisDetailApi = combAnalysisApi(() => {
      return this.$state.params
    }
    , TimeStore.getValue
    , function(){
        return {
          order:sortMap[this.viewKey]
        }
      }
    ,{
      size:1
    })
    this.combAnalysisDetailApi.fetch().then((data,params) => {
      this.$state.gridCols = viewGridCols[params.viewKey]
      this.$state.grid = {
        data:_.chain(data.result).map(function(i){
          return _.extend(propConfig[params.viewKey].parse(i.combData),i.combData,i.key?i.key:{
            id:i.id,
            name:i.name
          })
        }).value()
      }
      this.emit()
    },(err,params) => {
      this.$state.grid = {
        error:err
      }
      this.emit()
    },(params) => {
      this.$state.grid = {
        loading:true
      }
      this.emit()
    })
  }
  $indicator_change(payload){
    this.$state.params.classify = null
    this.$state.params.urlName = payload.value.urlName
    this.$state.params.viewKey = payload.value.viewKey
    this.$state.category = payload.category
    this.$state.dimOptions = dimOptionMap[payload.category]
    this.$state.filterConditions = filterConditionMap[payload.category]
    this._fetchOptions()
    this.emit()
  }
  $submit_conditions(payload){
    this.$state.params = payload.params
    this.fetch()
  }
  $settime(){
    this._fetchOptions().then(() => this.fetch())
  }
  $load(){
    this._fetchOptions()
    this.fetch()
  }
  _fetchOptions(){
    return $.when.apply($, this.$state.filterConditions.map((con) => {
      return con.fetch()
    })).then(() => {
      this.emit()
    })
  }
}
